updating oE routine_id

Indirect Routine Calling

Accessing Euphoria coded routines

routine_id

<built-in> function routine_id(sequence routine_name) 

returns an integer id number for a user-defined Euphoria procedure or function.

Parameters:
  1. routine_name : a string, the name of the procedure or function.
Returns:

An integer, known as a routine id, -1 if the named routine can't be found, else zero or more.

Errors:

routine_name should not exceed 1,024 characters.

Comments:

The id number can be passed to call_proc or call_func, to indirectly call the routine named by routine_name. This id depends on the internal process of parsing your code, not on routine_name.

The routine named routine_name must be visible (that is callable) at the place where routine_id is used to get the id number. If it is not, -1 is returned.

Indirect calls to the routine can appear earlier in the program than the definition of the routine, but the id number can only be obtained in code that comes after the definition of the routine - see example 2 below.

Once obtained, a valid routine id can be used at any place in the program to call a routine indirectly via call_proc or call_func, including at places where the routine is no longer in scope.

Some typical uses of routine_id are:

  1. Creating a subroutine that takes another routine as a parameter. (See Example 2 below)
  2. Using a sequence of routine id's to make a case (switch) statement. Using the switch statement is more efficient.
  3. Setting up an Object-Oriented system.
  4. Getting a routine id so you can pass it to call_back. (See Platform-Specific Issues)
  5. Getting a routine id so you can pass it to task_create. (See Multitasking in Euphoria)
  6. Calling a routine that is defined later in a program. This is no longer needed from v4.0 onward.

Note that C routines, callable by Euphoria, also have ids, but they cannot be used where routine ids are, because of the different type checking and other technical issues.

See Also:

define_c_proc and define_c_func

Example 1:
 procedure foo() 
    puts(1, "Hello World\n") 
end procedure 
 
integer foo_num 
foo_num = routine_id("foo") 
 
call_proc(foo_num, {})  -- same as calling foo() 
Example 2:
function apply_to_all(sequence s, integer f) 
    -- apply a function to all elements of a sequence 
    sequence result 
    result = {} 
    for i = 1 to length(s) do 
        -- we can call add1() here although it comes later in the program 
        result = append(result, call_func(f, {s[i]})) 
    end for 
    return result 
end function 
 
function add1(atom x) 
    return x + 1 
end function 
 
-- add1() is visible here, so we can ask for its routine id 
? apply_to_all({1, 2, 3}, routine_id("add1")) 
-- displays {2,3,4} 
See Also:

call_proc, call_func, call_back, define_c_func, define_c_proc, task_create, Platform-Specific Issues, Indirect routine calling

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu